package com.huaxin.hxcloud.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;


/**
 * @author songxf
 * @date 2017/11/24
 * @Desc 设置设备控制
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSetFacilityControlDialog extends Dialog implements View.OnClickListener {
    private Context             mContext;
    private String              mTitle;
    private OnItemClickListener mListener;
    private TextView            mTitleTv;
    private EditText            mInputPaswdEt;
    private TextView            mCancleTv;
    private TextView            mEnsureTv;

    public HxSetFacilityControlDialog(Context context, String title, OnItemClickListener listener) {
        super(context, R.style.Translucent_NoTitle);
        this.mContext = context;
        this.mTitle = title;
        this.mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_set_facility_control);
        Window win = getWindow();
        win.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        win.setGravity(Gravity.CENTER);
        initView();
        initData();
    }


    private void initView() {
        mTitleTv = (TextView) findViewById(R.id.title_id);
        mInputPaswdEt = (EditText) findViewById(R.id.input_paswd_et);
        mCancleTv = (TextView) findViewById(R.id.cancel_tv);
        mEnsureTv = (TextView) findViewById(R.id.ensure_tv);

    }

    private void initData() {
        mTitleTv.setText(("是否开启" + mTitle));
        mCancleTv.setOnClickListener(this);
        mEnsureTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_tv:
                if (HxSetFacilityControlDialog.this.isShowing()) {
                    HxSetFacilityControlDialog.this.cancel();
                }
                break;
            case R.id.ensure_tv:
                String paswdStr = mInputPaswdEt.getText().toString().trim();
                if (TextUtils.isEmpty(paswdStr)) {
                    ToastUtil.showToast("密码不能为空");
                    return;
                }
                mListener.onItemClick(paswdStr);
                break;
        }
    }

    public void setTitleText(String title) {
        mTitleTv.setText(title);
    }

    public interface OnItemClickListener {
        void onItemClick(String paswd);
    }
}
